package user;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

import cake.Cake;
import cake.UserDao;
import cake.UserDaoImpl;


@WebServlet(name = "AddServlet", value = "/AddServlet")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String data=req.getParameter("data");

        String id = req.getParameter("id");
        String shop = req.getParameter("shop");
        String detail = req.getParameter("detail");
        String price = req.getParameter("price");
        String num = req.getParameter("num");
        //实例化一个对象，组装属性
        Cake cake = new Cake();
        cake.setId(id);
        cake.setShop(shop);
        cake.setDetail(detail);
        cake.setPrice(price);
        cake.setNum(num);
        UserDao ud = new UserDaoImpl();
        if (ud.addcake(cake)){
            System.out.println("商品" + id + "新增成功");
            req.setAttribute("msg","商品" + id + "新增成功");
            req.getRequestDispatcher("cart.jsp").forward(req, resp);
        }else {
            resp.sendRedirect("error.jsp");
        }
    }
}
